package com.xuexiang.xui_lib.component.shadow;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.render.Canvas;
import ohos.app.Context;

/**
 * 可设置阴影的按钮
 *
 * @author XUE
 * @since 2019-03-30
 */
public class ShadowTextView extends Text implements Component.EstimateSizeListener {
    ShadowDrawable shadowDrawable;
    private DrawTask mDrawTask = (component, canvas) -> ShadowTextView.this.onDraw(component, canvas);

   /* *//**
     * 构造方法
     *
     * @param context 上下文
     *//*
    public ShadowTextView(Context context) {
        super(context);
        init(context, null);
    }*/

    /**
     * 构造方法
     *
     * @param context 上下文
     * @param attrs
     */
    public ShadowTextView(Context context, AttrSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    /**
     * 构造方法
     *
     * @param context
     * @param attrs
     * @param defStyleAttr
     */
    public ShadowTextView(Context context, AttrSet attrs, int defStyleAttr) {
        super(context, attrs, "");
        init(context, attrs);
    }

    private void init(Context context, AttrSet attrs) {
        shadowDrawable = ShadowDrawable.fromAttributeSet(context, attrs);
        setEstimateSizeListener(this);
    }

    @Override
    public boolean onEstimateSize(int widthEstimateConfig, int heightEstimateConfig) {
        int componentWidth = EstimateSpec.getSize(widthEstimateConfig);
        int componetHeight = EstimateSpec.getSize(heightEstimateConfig);
        setEstimatedSize(EstimateSpec.getChildSizeWithMode(componentWidth, componentWidth, EstimateSpec.NOT_EXCEED),
            EstimateSpec.getChildSizeWithMode(componetHeight, componetHeight, EstimateSpec.NOT_EXCEED));
        shadowDrawable.initEstimateSize(componentWidth, componetHeight);
        addDrawTask(mDrawTask);
        return true;
    }

    private void onDraw(Component component, Canvas canvas) {
        ShadowDrawable.ViewParams viewParams = new ShadowDrawable.ViewParams()
            .setPaddingLeft(getPaddingLeft())
            .setPaddingTop(getPaddingTop())
            .setText(getText())
            .setTextColor(getTextColor())
            .setTextSize(getTextSize());
        shadowDrawable.onDraw(canvas, viewParams);
    }
}
